<template>
  <!-- 电催案件管理-个人案件池-页面 -->
  <section class="page tel-person-case-manage">
    <data-form :rules="rules" :model="model" @onSearch="refreshData" @handleResetForm="handleResetForm" :page="pageService">
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="model.sysCode" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构" prop="deptIds">
          <el-cascader v-model="model.deptIds" :options="callDepartmentData" @change="getcupoName" :props="{value:'id',label:'deptName'}" change-on-select :show-all-levels="false" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="model.productName"></el-input>
        </el-form-item>
        <!-- <el-form-item label="顾客类型" prop="customerCategory">
          <el-select v-model="model.customerCategory" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CustomerCategory')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="案件金额" prop="overdueAmount">
          <number-range v-model="model.overdueAmount" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="逾期阶段" prop="overduePeriod">
          <el-select v-model="model.overduePeriod" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="model.overdueDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker type="daterange" :editable="false" v-model="model.followInTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-cascader v-model="model.area" :options="$city.getCityData()" change-on-select clearable></el-cascader>
        </el-form-item>
        <el-form-item label="案件标记" prop="caseMark">
          <el-select v-model="model.caseMark" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CaseMark')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
            <el-form-item label="是否强制入催" prop="forceCollection">
          <el-select v-model="model.forceCollection" placeholder="请选择" clearable>
            <el-option label="是" value="YES"></el-option>
            <el-option label="否" value="NO"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行动代码" prop="followupBack">
          <el-select v-model="model.followupBack">
            <el-option v-for="{name,id} in customDataDict.filter(v=>v.dataType==='FOLLOW_UP').filter(m=>m.name!=='行动代码')" :key="id" :label="name" :value="id">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="queryPageInfo" :sort="sortService" :page="pageService" ref="data-box" showCaseMark>
      <template slot="buttons">
        <el-button @click="advanceCirculation" v-auth = "209">申请流转</el-button>
      </template>
      <template slot="append">
        <case-marker @change-color="onColorChange" :selectionList="selectionList"></case-marker>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :formatter="(row)=>$filter.encryptPhone(row.mobileNo)" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" sortable :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="overdueAmount" label="案件金额(元)" sortable :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column v-if = 'dataSet[0] && dataSet[0].caseModelFlag == "Y"' prop="custModelScore" label="客户评分"  :min-width="$helper.getColumnWidth(3)">
        <template v-if = 'scope.row.caseModelFlag == "Y"'  slot-scope="scope">
      <span>{{scope.row.custModelScore != null ? scope.row.custModelScore.toFixed(2) : ''}}</span>
     </template>
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期阶段" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" sortable :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="followupTime" label="跟进时间" :formatter="(row) => $filter.dateTimeFormat(row.followupTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="followupBack" label="行动码" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.dictcustomConvert(row.followupBack)">
        </el-table-column>
        <el-table-column prop="area" label="地区" :min-width="$helper.getColumnWidth(4)" :formatter="(row)=> $city.getInheritNames(Number(row.area))" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :formatter="(row) => $filter.encryptSysCode(row.sysCode)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="perDueDate" label="还款日" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="currentCollectorRealName" label="永久催收员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="tempCollectorRealName" label="临时催收员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="lockCollectorRealName" label="锁定人员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="turnSource" label="流转来源" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.dictConvert(row.turnSource,'TurnSource')">
        </el-table-column>
        <el-table-column prop="recordFollowInTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.recordFollowInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="distributeTime" label="案件分配时间" sortable :formatter="(row) => $filter.dateTimeFormat(row.distributeTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="leaveCaseFlag" label="留案标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.leaveCaseFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="leaveDueDate" label="留案到期时间" :min-width="$helper.getColumnWidth(5)" :formatter="(row) => $filter.dateFormat(row.leaveDueDate)">
        </el-table-column>
        <el-table-column prop="forceCollection" label="是否强制入催" :min-width="$helper.getColumnWidth(5)">
          <template slot-scope="scope">
           <span>{{scope.row.forceCollection==='YES'?'是':'否'}}</span>
          </template>
        </el-table-column>
      </template>
      <p slot="footer">{{`查询结果统计：列表逾期总金额 ${$filter.toThousands(totalInfo.totalAmount)}元`}}</p>
    </data-box>

    <!--申请弹框-->
    <el-dialog :title="`${applyType.name}申请`" width="800px" :visible.sync="dialog.apply" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <apply :caseIds="selectedCaseIds" :fromChannel="caseChannel" :applyType="applyType.value" @close="dialog.apply = false;refreshData()" ref="apply-circulation"></apply>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import NumberRange from "~/components/common/number-range.vue";
import { TelCaseManageService } from "~/services/domain-service/tel-case-manage.service";
import CaseMarker from '~/components/common/case-marker.vue';
import Apply from "~/components/visit-case/apply.vue";
import { ApplyType, CASE_CHANNEL } from "~/config/enum.config";
import { State, Getter, Mutation, namespace } from "vuex-class";
import { CaseInfoService } from '~/services/domain-service/case-info.service';
import { OpenExecutionData } from "~/config/types";
const CollectionExecutionModule = namespace("collection-execution");

@Layout("workspace")
@Component({
  components: {
    DataForm,
    DataBox,
    NumberRange,
    CaseMarker,
    Apply
  }
})
export default class TelPersonCaseManage extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(TelCaseManageService)
  private telCaseManageService: TelCaseManageService;
  @Dependencies(CaseInfoService)
  private caseInfoService: CaseInfoService;
  @Mutation
  private updateCollectionType;
  @CollectionExecutionModule.Action
  private openCollectionExecution;
  @Getter
  private callDepartmentData;
  @State
  private customDataDict;

  private model: any = {
    caseNumber: "",
    personalName: "",
    mobileNo: "",
    idCard: "",
    deptId: "",
    deptIds: [],
    customerCategory: "",
    sysCode: "",
    productName: "",
    overdueAmount: {
      max: "",
      min: ""
    },
    overduePeriod: "",
    overdueDays: {
      max: "",
      min: ""
    },
    followInTime: '',
    area: [],
    forceCollection:"",
    caseMark: "",
    followupBack: ''
  };
  private rules: any = {};
  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private caseMark: any = "";
  private dialog: any = {
    apply: false
  };
  private applyType = ApplyType.turn;
  private caseChannel = CASE_CHANNEL.tel;
  private totalInfo: any = {}
  private mounted() {
    this.refreshData();
  }
  private activated() {
    this.refreshData();
  }

  /**
   * 查询统计信息
   */
  private queryTotalInfo() {
    this.telCaseManageService.personalCasePoolAmount(this.model).subscribe(data => this.totalInfo = data)
  }

  /**
   * 查询翻页信息
   */
  private queryPageInfo() {
    this.selectionList = []
    this.telCaseManageService
      .personalCasePool(this.model, this.pageService)
      .subscribe(data => {
        this.dataSet = data
        if(this.dataSet){
          this.dataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
      });
  }

  private refreshData() {
    this.queryTotalInfo()
    this.queryPageInfo()
  }

  /**
   * 表单范围输入框重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: "", max: "" };
    this.model.overdueDays = { min: "", max: "" };
    this.model.deptId = ''
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.openCollectionExecution(
      new OpenExecutionData(row.id, row.caseNumber, CASE_CHANNEL.tel)
    );
  }

  /**
   * 申请流转
   */
  private advanceCirculation() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要流转的案件");
      return;
    }
    this.dialog.apply = true;
  }

  private get selectedCaseIds() {
    return this.selectionList.map(v => v.id);
  }

  /**
   * 获取机构id
   */
  private getcupoName() {
    this.model.deptId =
      this.model.deptIds.length > 0
        ? this.model.deptIds[this.model.deptIds.length - 1]
        : "";
  }

  private onColorChange(val) {
    const colorModel: any = {
      caseIds: this.selectionList.map(v => v.id),
      caseMark: val,
    };
    this.caseInfoService.caseInfoMarkColor(colorModel).subscribe(this.refreshData);
  }
}
</script>

<style lang="less" scoped>
</style>
